package com.woniuxy.caradmin.service.impl;

import com.woniuxy.caradmin.entity.pojo.Permission;
import com.woniuxy.caradmin.mapper.PermissionMapper;
import com.woniuxy.caradmin.mapper.RoleMapper;
import com.woniuxy.caradmin.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public List<Permission> selectPermissionsByUid(Integer uid) {

        List<Permission> permissions = permissionMapper.selectPermissionsByUid(uid);
        return permissions;
    }

    @Override
    public List<Permission> selectAllPermission() {
        return permissionMapper.selectAllPermission();
    }

    @Override
    public Integer[] selectPermissionsByRid(Integer rid) {
        List<Permission> permissions = permissionMapper.selectPermissionsByRid(rid);
        Integer[] per=new Integer[permissions.size()];
        if (permissions != null&&permissions.size()>0){
            for (int i=0; i<permissions.size();i++){
                per[i]=permissions.get(i).getId();
            }
        }
        return per;
    }

    @Override
    public boolean assignRight(Integer rid, Integer[] perIds) {
        boolean flag=false;
        Integer delcount = roleMapper.delRolePerByRid(rid);
        if(perIds!=null&&perIds.length>0){
            for(Integer perId:perIds){
                Map<String,Integer> map=new HashMap<>();
                map.put("rid",rid);
                map.put("perid",perId);
                roleMapper.addRoleAndPer(map);
            }
        }
        flag=true;
        return flag;

    }
}
